package model.entity;

import java.util.UUID;

import model.crafting.EntityCraftingTable;
import model.inventory.Inventory;
import model.inventory.item.Item;
import model.world.Chunk;
import model.world.World;
import model.world.tile.ResourceType;
import model.world.tile.Tile;

public abstract class Entity {
	int baseCollectionRate;
	int entityID;
	protected UUID GUID;
	protected Tile currentTile;
	protected Inventory inventory;
	protected EntityCraftingTable craftingTable;
	protected World world;

	protected Entity(World world, Tile parent, int entityID){
		GUID = UUID.randomUUID();
		currentTile = parent;
		inventory = new Inventory();
		this.entityID = entityID;
	}

	public Chunk getParentChunk(){
		return currentTile.getParentChunk();
	}

	public abstract int getResourceCollectionRate(ResourceType t);

	public long getX(){
		return currentTile.getX();
	}

	public long getY(){
		return currentTile.getY();
	}

	public EntityCraftingTable getCraftingTable(){
		return craftingTable;
	}

	public void giveInventory(Inventory transaction) {
		inventory.add(transaction);
	}

	public abstract void tick();

	public int getID() {
		return entityID;
	}

	public Inventory getInventory(){
		return inventory;
	}

	public boolean isWorking() {
		return currentTile.getOccupyingEntity() == this;
	}

	public Tile getCurrentTile() {
		return currentTile;
	}

	public UUID getGUID() {
		return GUID;
	}

	public void addToCrafting(int itemID, int x, int y){
		if(x >= 0 && x < craftingTable.getWidth() && y >= 0 && y < craftingTable.getHeight()){
			Item item = inventory.findItem(itemID);
			if(item != null)
				inventory.add(craftingTable.addItem(x, y, inventory.remove(new Item(item.getID(), 1))));
		}
	}

	public void addSelectedItemToCrafting(int x, int y){
		if(inventory.getSelectedItem() != null)
			addToCrafting(inventory.getSelectedItem().getID(), x, y);
	}

	public void takeCraftingOutput() {
		if(craftingTable.getOutput() != null){
			Item output = craftingTable.removeOutput();
			inventory.add(output);
		}
	}

	public void removeCraftingItem(long x, long y) {
		inventory.add(craftingTable.removeItem((int)x, (int)y));
	}
}
